package com.shfl.servermanage.logic.api;

import com.shfl.servermanage.bt.BtComponent;
import com.shfl.servermanage.bt.BtConstants;
import com.shfl.servermanage.common.ApiResultHelper;
import com.shfl.servermanage.common.ApiResultSimpleHelper;
import com.shfl.servermanage.common.SystemConstant;
import com.shfl.servermanage.logic.model.info.BtUserInfo;
import com.shfl.servermanage.logic.model.request.EditServerBtRequest;
import com.shfl.servermanage.logic.model.request.TestServerBtRequest;
import com.shfl.servermanage.logic.model.response.ServerBtDetailResponse;
import com.shfl.servermanage.logic.service.SysServerBtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/server/bt")
@Api(value = "宝塔服务器", description = "宝塔服务器")
public class SysServerBtController {

    @Autowired
    private BtComponent btComponent;
    @Autowired
    private SysServerBtService sysServerBtService;

    @GetMapping("detail/by/server")
    @ApiOperation(value = "宝塔服务详情", notes = "宝塔服务详情")
    public ApiResultSimpleHelper<ServerBtDetailResponse> detail(
            @ApiParam(name = "serverId", example = "1", value = "服务器Id", required = true)
            @RequestParam(value = "serverId") Long serverId
    ) {
        ServerBtDetailResponse serverBtDetailResponse = new ServerBtDetailResponse();
        sysServerBtService.findByServerId(serverBtDetailResponse, serverId);
        return new ApiResultSimpleHelper<>(SystemConstant.RE_SUCCESS_CODE, serverBtDetailResponse);
    }

    @PostMapping(value = "/test")
    @ApiOperation(value = "测试连接", notes = "测试连接")
    public ApiResultHelper test(
            @RequestBody TestServerBtRequest testServerBtRequest
    ) throws Exception {
        BtUserInfo info = new BtUserInfo();
        info.setSecretKey(testServerBtRequest.getBtSign());
        info.setAddress(testServerBtRequest.getBtUrl());
        info.setApiUrl(BtConstants.SYSTEM_TOTAL);
        btComponent.send(info, null);
        return new ApiResultHelper(SystemConstant.RE_SUCCESS_CODE);
    }

    @PostMapping(value = "/edit")
    @ApiOperation(value = "编辑", notes = "编辑")
    public ApiResultHelper edit(
            @RequestBody EditServerBtRequest editServerBtRequest
    ) throws Exception {
        sysServerBtService.edit(editServerBtRequest);
        return new ApiResultHelper(SystemConstant.RE_SUCCESS_CODE);
    }

}
